//
//  BaseVC.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import UIKit
import AxcBedrock

class BaseVC: BBaseVC {
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    /// - Returns: 视图
    static func CreateFromNib(name: String? = nil,
                              bundle: Bundle? = nil,
                              frame: CGRect = .zero) -> Self {
        var nibName: String = NSStringFromClass(Self.self)
        if let name = name {
            nibName = name
        }
        let nib = AxcBedrockNib.Axc.Create(nibName: nibName, bundle: .main)
        let objs = nib.instantiate(withOwner: nil, options: nil)

        var findObj: Self?
        objs.forEach { obj in
            if let _obj = obj as? Self {
                findObj = _obj
            }
        }
        guard let findObj else {
            return Self() // 默认走实例化方法
        }
        return findObj
    }

    enum NavStyle {
        // 白色

        case white

        // 深色

        case dark

        // 透明的

        case transparent(tintColor: UIColor)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        createUI()

        Self.instanceFactory()

        bindReverseAction() // 反向数据流绑定

        bindForwardAction() // 正向数据流绑定
    }

    /// 导航栏细线
    lazy var naviLineView: UIView = {
        let view = UIView()

        view.backgroundColor = "E6E6E6".axc.uiColor

        return view
    }()

    /// 标题
    override var title: String? {
        set { navView.title = newValue }

        get { return navView.title }
    }

    /// 禁用三指键盘撤回
    @available(iOS 13.0, *)
    override var editingInteractionConfiguration: UIEditingInteractionConfiguration {
        return .none
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if isNavViewLoad { // 如果加载了导航条视图，则每次布局前将其前置
            view.bringSubviewToFront(navView)
        }
    }

    /// Internal
    /// navView是否加载
    fileprivate var isNavViewLoad: Bool = false

    static var NavHeight: CGFloat { 50 + StatusBarHeight }

    /// 自定义导航条
    lazy var navView: NavView = {
        let navView = NavView()

        navView.backControl.addTarget(self, action: #selector(self.backAction), for: .touchUpInside)

        return navView
    }()

    /// 状态栏颜色
    var statusBarStyle: UIStatusBarStyle = .default

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return statusBarStyle
    }

    /// 创建导航
    func makeNavView(style: NavStyle = .white, showShadow: Bool = true) {
        switch style {
        case .white:

            navView.backgroundColor = .white

            navView.backImageView.axc.set(tintColor: "323233".axc.uiColor)

            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            navView.headlineLabel.textColor = "323233".axc.uiColor

            statusBarStyle = .default

        case .dark:

            navView.backgroundColor = .black

            navView.backImageView.axc.set(tintColor: UIColor.white)

            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            navView.headlineLabel.textColor = .white

            statusBarStyle = .lightContent

        case let .transparent(tintColor):

            navView.backgroundColor = .clear

            navView.backImageView.axc.set(tintColor: tintColor)

            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            navView.headlineLabel.textColor = tintColor

            statusBarStyle = .default
        }

        setNeedsStatusBarAppearanceUpdate()

        setNeedsStatusBarAppearanceUpdate()

        isNavViewLoad = true

        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        addSubview(navView)

        navView.layer.zPosition = 10 // 永远前置

        navView.snp.makeConstraints { make in

            make.top.left.right.equalToSuperview()

            make.height.equalTo(Self.NavHeight)
        }

        if showShadow {
            navView.addShadow()
        }
    }

    /// 导航视图
    var navigationVC: NavVC? {
        guard let nav = navigationController as? NavVC else { return nil }

        return nav
    }

    /// 返回的操作事件
    @objc
    func backAction() {
        navigationController?.popViewController(animated: true)
    }

    /// dismiss返回
    func dismissVC(animated: Bool = true) {
        dismiss(animated: true, completion: nil)
    }

    var keyboardManagerEnable: Bool = true {
        didSet {
            IQKeyboardManager.shared().isEnabled = keyboardManagerEnable

            IQKeyboardManager.shared().shouldResignOnTouchOutside = keyboardManagerEnable
        }
    }

    /// 方便添加视图
    func addSubview(_ subView: UIView) {
        view.addSubview(subView)
    }

    /// 正向数据流（页面操作的监听）方法放在这里
    func bindForwardAction() { }

    /// 反向数据流（ViewModel的监听）进行数据绑定的放在这里
    func bindReverseAction() { }

    /// 创建UI的方法放在这里
    /// 最好按照，从上至下，从左至右依次添加视图
    func createUI() { }
}
